/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import { getStatus, getOpen } from "../services/api";

export default {
  namespace: "mall",

  state: {
    stateType: null,
    pagination: null,
    refused: null,
    hrefUrl: null
  },

  effects: {
    * getStatus({ payload }, { call, put }) {
      const response = yield call(getStatus, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { status, response: refused, url } = data;
      if (code == 1) {
        if (msg == "申请成功") {
          yield put({
            type: "save",
            payload: {
              pagination: msg,
              stateType: "审核中"
            }
          });
          message.success(msg);
          yield call(getStatus, "");
        } else {
          message.error(msg);
        }
      } else if (code == 0 && status == "拒绝") {
        yield put({
          type: "save",
          payload: {
            stateType: status,
            refused
          }
        });
      } else {
        yield put({
          type: "save",
          payload: {
            stateType: status,
            hrefUrl: url
          }
        });
      }
    },
    * getOpen({ payload }, { call, put }) {
      const response = yield call(getOpen, payload);
      if (!response) return;
      const { msg } = response;
      if (msg == "申请成功") {
        yield put({
          type: "save",
          payload: {
            pagination: msg,
            stateType: "审核中"
          }
        });
        message.success(msg);
        yield call(getStatus, payload);
      } else {
        message.error(msg);
      }
    }
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
};
